package com.szhn.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.szhn.aclservice.entity.AclPermission;
import com.szhn.aclservice.entity.AclRolePermission;
import com.szhn.aclservice.entity.vo.AclPermissionVo;
import com.szhn.aclservice.mapper.AclPermissionMapper;
import com.szhn.aclservice.service.AclPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szhn.aclservice.service.AclRolePermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-28
 */
@Service
public class AclPermissionServiceImpl extends ServiceImpl<AclPermissionMapper, AclPermission> implements AclPermissionService {

    @Autowired
    private AclRolePermissionService aclRolePermissionService;

    //===================递归查询======================================
    @Override
    public List<AclPermissionVo> getAll() {
        //查询菜单中所有数据
        List<AclPermission> permissionList = baseMapper.selectList(null);

        //创建list集合做最终数据封装
        ArrayList<AclPermissionVo> finalList = new ArrayList<>();

        //将查询出来的数据封装到自定义javabean中
        ArrayList<AclPermissionVo> aclPermissionVos = new ArrayList<>();
        for (AclPermission aclPermissions : permissionList) {
            AclPermissionVo aclPermissionVo = new AclPermissionVo();
            BeanUtils.copyProperties(aclPermissions, aclPermissionVo);
            aclPermissionVos.add(aclPermissionVo);

        }
        //遍历list找到第一层
        for (AclPermissionVo permissionNode : aclPermissionVos) {
            if ("0".equals(permissionNode.getPid())) {
                //设置顶层菜单level是1
                permissionNode.setLevel(1);
                //根据顶层菜单，向里面进行子查询，封装到finalList中
                finalList.add(selectChildren(permissionNode, aclPermissionVos));
            }

        }
        return finalList;
    }

    /*递归查询*/
    private AclPermissionVo selectChildren(AclPermissionVo permissionNode, List<AclPermissionVo> aclPermissionVos) {
        //向一层菜单中二层菜单，二层菜单中放三层......,先赋空
        permissionNode.setChildren(new ArrayList<AclPermissionVo>());
        //得到一层下的二层菜单,遍历id和pid相等
        for (AclPermissionVo item : aclPermissionVos) {
            if (permissionNode.getId().equals(item.getPid())) {
                //父菜单的level值+1
                int level = permissionNode.getLevel() + 1;
                item.setLevel(level);

                //把查询出来的子菜单放到父菜单
                permissionNode.getChildren().add(selectChildren(item, aclPermissionVos));
            }

        }
        return permissionNode;
    }


    //=====================递归删除================
    @Override
    public void deleteById(String id) {
        //创建集合封装所有要删除的菜单id值
        ArrayList<String> idLists = new ArrayList<>();
        //调用方法，递归删除
        this.deleteChildren(id, idLists);
        //将当前id封装
        idLists.add(id);
        baseMapper.deleteBatchIds(idLists);
    }


    private void deleteChildren(String id, ArrayList<String> idLists) {
        QueryWrapper<AclPermission> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        //只查询pid=id的id字段
        wrapper.select("id");
        List<AclPermission> childrenList = baseMapper.selectList(wrapper);
        //查询子中的子
        for (AclPermission item : childrenList) {
            idLists.add(item.getId());
            this.deleteChildren(item.getId(), idLists);
        }
    }

    //==================给角色分配菜单========================
    @Override
    public void saveRoleAndPermission(String roleId, String[] permissionId) {
        //1 创建list集合，用于封装添加数据
        List<AclRolePermission> rolePermissionList = new ArrayList<>();
        //遍历所有菜单数组
        for (String perId : permissionId) {
            //RolePermission对象
            AclRolePermission rolePermission = new AclRolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(perId);
            //封装到list集合
            rolePermissionList.add(rolePermission);
        }
        //添加到角色菜单关系表
        aclRolePermissionService.saveBatch(rolePermissionList);
    }


}
